<?php

namespace App\Controller\Api;

use App\Controller\AbstractController;
use App\Service\Code\Normal;
use App\Service\OrderService\OrderNotifyService\PddNotifyService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Contract\RequestInterface;

/**
 * @Controller(prefix="/YunCangPddReloadOrder")
 * @author luoshumai
 */
class YunCangPddReloadOrderController extends AbstractController
{
    /**
     * 重新捞取拼多多订单
     * @RequestMapping(path="reloadOrder", methods="post")
     * @return string[]
     */
    public function reloadOrder(RequestInterface $request, $CustomOrderTag)
    {
        $orderInfos = $request->post('orders');
        if( empty($orderInfos) ) {
            return Normal::returnCode(Normal::FAIL,'参数orders不能为空');
        }
        
        if( !is_array($orderInfos) ) {
            return Normal::returnCode(Normal::FAIL,'参数orders必须为数组');
        }
        
        $orderInfo = current($orderInfos);
        if( empty($orderInfo['storeId'])) {
            return Normal::returnCode(Normal::FAIL,'参数orders.storeId不能为空');
        }
        
        if( empty($orderInfo['orderSn'])) {
            return Normal::returnCode(Normal::FAIL,'参数orders.orderSn不能为空');
        }
        
        $orderSnChunk = array_chunk($orderInfos, 50);
        $result_all = [];
        foreach ($orderSnChunk as $chunk) 
        {
            $result = PddNotifyService::instance()->getBatchOrder($chunk);
            $result_all = array_merge($result_all,$result);
        }
        return Normal::returnCode(Normal::SUC,'处理批量捞取订单完成，请根据data里面的二维数组list遍历处理每个订单详细结果![code-200代表捞单成功，code-0代表捞单失败，msg-为具体返回备注，data-为具体返回值]',$result_all);
    }
}